import { 
  getBranchTreeList ,
  getPostListByBranchId, 
  addJobs, 
  jobsDetails, 
  changeJobsDetails,
  deleteRole,
  deleteBranch,
  getParentBranchList,
  update,
  addOrganization,
  getParentBranch,
  deleteJobs,
  getEditBranchList
} from '../services/api';
import { message } from 'antd';
import { updateLocale } from 'moment';

export default {
  namespace: 'structure',

  state: {
      branchList: [],
      jobsDetailsList: [],
      jobsList: [],
      roleList: [],
      page:[],
      superiorList: [],
      editBranchList: [],
      superiorBranchList: [],
      loading: false,
      branchLoading: false,
      deleteJobsLoading: false
  },

  effects: {

    *fetch({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(getBranchTreeList, payload);
      let q = response.data[0]
      yield put({
        type: 'query',
        payload: {
          branchList: response.data,
          loading: false,
        },
        
      });
      if(callback)callback()
    },

    *jobs({ payload }, { call, put }) { 
      // console.log("jobs1",payload)
      yield put({
        type: 'query',
        payload: {
          deleteJobsLoading: true,
        },
      });
      const response = yield call(getPostListByBranchId, payload);
      // console.log("jobs2",response)
      yield put({
        type: 'query',
        payload: {
          jobsList: response.data,
          deleteJobsLoading: false,
        },
        
      });
    },

    *addJobs({ payload,callback }, { call, put }) {
      // console.log("addJobs1",payload)
      yield put({
        type: 'query',
        payload: {
          deleteJobsLoading: true,
        },
      });
      const response = yield call(addJobs, payload);
      // console.log("addJobs2",response)
      yield put({
        type: 'query',
        payload: {
          deleteJobsLoading: false,
        },
      });
      if(response.result.code=="200"){
        message.success('添加成功');
      }else{
        message.error(response.result.message);
      }
      yield put({
        type: 'query',
        payload: {
          data: response
        },      
      });
      if(callback)callback();
    },

    *jobsDetails({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(jobsDetails, payload);
      yield put({
        type: 'query',
        payload: {
          jobsDetailsList: response.data.post,
          roleList: response.data.roleList,
          loading: false,
        },      
      });
      if(callback)callback()
    },

    *changeJobsDetails({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(changeJobsDetails, payload);
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });

      if(response.result.code=="200"){
        message.success('修改成功');
      }else{
        message.error(response.result.message);
      }
      yield put({
        type: 'query',
        payload: {
          data: response,
        },      
      });
      if(callback)callback();
    },

    *deleteRole({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });
      const response = yield call(deleteRole, payload);
      yield put({
        type: 'query',
        payload: {
          loading: true,
        },
      });

      if(response.result.code=="200"){
        message.success('删除成功');
      }else{
        message.error(response.result.message);
      }
      yield put({
        type: 'query',
        payload: {
          data: response,
        },      
      });
      if(callback)callback();
    },

    *deleteBranch({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          branchLoading: true,
        },
      });
      const response = yield call(deleteBranch, payload);
      yield put({
        type: 'query',
        payload: {
          branchLoading: true,
        },
      });

      if(response.result.code=="200"){
        message.success('删除成功');
      }else{
        message.error(response.result.message);
      }
      yield put({
        type: 'query',
        payload: {
          data: response,
        },      
      });
      if(callback)callback();
    },

    *getParentBranchList({ payload ,callback}, { call, put }) {
      const response = yield call(getParentBranchList, payload);
      yield put({
        type: 'query',
        payload: {
          superiorList: response.data,
        },      
      });
      if(callback)callback();
    },
    
    *getEditBranchList({ payload ,callback}, { call, put }) {
      const response = yield call(getEditBranchList, payload);
      yield put({
        type: 'query',
        payload: {
          editBranchList: response.data,
        },      
      });
      if(callback)callback();
    },


    *getParentBranch({ payload ,callback}, { call, put }) {
      const response = yield call(getParentBranch, payload);
      yield put({
        type: 'query',
        payload: {
          superiorBranchList: response.data,
        },      
      });
      if(callback)callback();
    },

    *update({ payload ,callback}, { call, put }) {
     
      yield put({
        type: 'query',
        payload: {
          branchLoading: true,
        },
      });
      const response = yield call(update, payload);
      
      yield put({
        type: 'query',
        payload: {
          branchLoading: false,
        },
      });
      if(response.result.code=="200"){
        message.success('修改成功');
      }else{
        message.error(response.result.message);
      }
      // yield put({
      //   type: 'query',
      //   payload: {
      //     updataBranchList: response.data,
      //   },      
      // });
      if(callback)callback();
    },

    *addOrganization({ payload ,callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          branchLoading: true,
        },
      });
      const response = yield call(addOrganization, payload);
      
      yield put({
        type: 'query',
        payload: {
          branchLoading: false,
        },
      });
      if(response.result.code=="200"){
        message.success('添加成功');
      }else{
        message.error(response.result.message);
      }
      if(callback)callback();
    },

    *deleteJobs({ payload, callback}, { call, put }) {
      yield put({
        type: 'query',
        payload: {
          deleteJobsLoading: true,
        },
      });
      const response = yield call(deleteJobs, payload);
      yield put({
        type: 'query',
        payload: {
          deleteJobsLoading: false,
        },
      });
      if(response.result.code=="200"){
        message.success('删除成功');
        
      }else{
        message.error(response.result.message);
      }
      yield put({
        type: 'query',
        payload: {
          data: response,
        },      
      });
      if(callback)callback();
    },
  },
  

  reducers: {
    query(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
